package com.itlong.cloud.rabbitmq.config;

import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.rabbitmq.consumer.WebSocketPushMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * <desc>
 *
 * </desc>
 *
 * @createDate  2021/7/1
 */
@Configuration
public class RedisSubscriberConfig {
    /**
     * 消息监听适配器，注入接受消息方法
     *
     * @param receiver
     * @return
     */
    @Bean
    public MessageListenerAdapter messageListenerAdapter(WebSocketPushMessageListener receiver) {
        return new MessageListenerAdapter(receiver);
    }

    /**
     * 创建消息监听容器
     *
     * @param redisConnectionFactory
     * @param messageListenerAdapter
     * @return
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        //监听发布者
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic(RedisConstant.WEBSOCKET_REDIS_MQ_TOPIC));
        return redisMessageListenerContainer;
    }
}